package com.lighthouse.common.tag.controller;

import com.lighthouse.common.tag.domain.KnowledgeTag;
import com.lighthouse.common.tag.domain.KnowledgeTagTree;
import com.lighthouse.common.tag.service.IKnowledgeTagService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 基础标签Controller
 * 
 * @author ysz
 * @date 2023-03-22
 */
@RestController
@RequestMapping("/knowledge/tag")
public class KnowledgeTagController extends BaseController
{
    @Autowired
    private IKnowledgeTagService knowledgeTagService;

    /**
     * 查询基础标签列表
     */
    @PreAuthorize("@ss.hasPermi('common:tag:list')")
    @GetMapping("/list")
    public TableDataInfo list(KnowledgeTag baseTag)
    {
        startPage();
        List<KnowledgeTag> list = knowledgeTagService.selectBaseTagList(baseTag);
        return getDataTable(list);
    }

    /**
     * 查询基础标签列表-树形
     */
    @PreAuthorize("@ss.hasPermi('common:tag:list')")
    @GetMapping("/tree")
    public TableDataInfo tree(KnowledgeTag baseTag)
    {
//        startPage();
        List<KnowledgeTagTree> list = knowledgeTagService.selectBaseTagTree();
        return getDataTable(list);
    }

    /**
     * 导出基础标签列表
     */
    @PreAuthorize("@ss.hasPermi('common:tag:export')")
    @Log(title = "基础标签", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, KnowledgeTag baseTag)
    {
        List<KnowledgeTag> list = knowledgeTagService.selectBaseTagList(baseTag);
        ExcelUtil<KnowledgeTag> util = new ExcelUtil<KnowledgeTag>(KnowledgeTag.class);
        util.exportExcel(response, list, "基础标签数据");
    }

    /**
     * 获取基础标签详细信息
     */
    @PreAuthorize("@ss.hasPermi('common:tag:query')")
    @GetMapping(value = "/{tagId}")
    public AjaxResult getInfo(@PathVariable("tagId") Long tagId)
    {
        return success(knowledgeTagService.selectBaseTagByTagId(tagId));
    }

    /**
     * 新增基础标签
     */
    @PreAuthorize("@ss.hasPermi('common:tag:add')")
    @Log(title = "基础标签", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody KnowledgeTag baseTag)
    {
        return toAjax(knowledgeTagService.insertBaseTag(baseTag));
    }

    /**
     * 修改基础标签
     */
    @PreAuthorize("@ss.hasPermi('common:tag:edit')")
    @Log(title = "基础标签", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody KnowledgeTag baseTag)
    {
        return toAjax(knowledgeTagService.updateBaseTag(baseTag));
    }

    /**
     * 删除基础标签
     */
    @PreAuthorize("@ss.hasPermi('common:tag:remove')")
    @Log(title = "基础标签", businessType = BusinessType.DELETE)
	@DeleteMapping("/{tagIds}")
    public AjaxResult remove(@PathVariable Long[] tagIds)
    {
        return toAjax(knowledgeTagService.deleteBaseTagByTagIds(tagIds));
    }
}
